//
//  HYHomeBaseCollectionCell.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/8.
//

#import "HYHomeBaseCollectionCell.h"

@interface HYHomeBaseCollectionCell ()

@property (nonatomic, assign) BOOL isTouching;

@end

@implementation HYHomeBaseCollectionCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self configUI];
    }
    return self;
}

- (void)configUI
{
    self.titleLabel.font = kIsIPad?FONT_BOLD_SIZE(14):FONT_SIZE(14);
    [self.contentView addSubview:self.backView];
    [self.backView addSubview:self.coverView];
    [self.backView addSubview:self.iconView];
    [self.backView addSubview:self.titleLabel];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.backView).offset(5);
        make.right.mas_equalTo(-5);
        make.bottom.mas_offset(-25);
    }];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.backView);
        make.width.height.mas_equalTo(44);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.backView);
        make.height.mas_equalTo(20);
    }];
}

- (void)addLongPressGesture
{
    // 长按手势
    UILongPressGestureRecognizer *pressGes = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
    pressGes.minimumPressDuration = 1.0;
    [self.contentView addGestureRecognizer:pressGes];
}

- (void)bindViewModel:(HYHomeFileItemViewModel *)vm
{
    _itemVm = vm;
    self.titleLabel.text = vm.fileName;
    self.lockView.hidden = !vm.isLock;
}

#pragma mark - 点击

- (void)handleLongPress:(UILongPressGestureRecognizer *)longPressGes
{
    if(longPressGes.state == UIGestureRecognizerStateBegan && !self.isTouching){
        self.isTouching = YES;
        [self startScaleAnimation];
        [self.itemVm.setSubject sendNext:self.itemVm];
    }else if(longPressGes.state == UIGestureRecognizerStateEnded){
        self.isTouching = NO;
    }
}

/** 设置缩放动画 */
- (void)startScaleAnimation
{
    // 触发震动反馈
    [CommUtls touchImpactFeedbackGenerator];
    // 放大动画
    [UIView animateWithDuration:0.2 animations:^{
        self.transform = CGAffineTransformMakeScale(1.1, 1.1); //等比放大1.2倍
    } completion:^(BOOL finished) {
        // 缩回去动画
        [UIView animateWithDuration:0.1 animations:^{
            self.transform = CGAffineTransformIdentity; //回复原始状态
        } completion:^(BOOL finished) {
            // 放大动画
            [UIView animateWithDuration:0.08 animations:^{
                self.transform = CGAffineTransformMakeScale(1.1, 1.1); //等比放大1.1倍
            } completion:^(BOOL finished) {
                // 缩回去动画
                [UIView animateWithDuration:0.04 animations:^{
                    self.transform = CGAffineTransformIdentity; //回复原始状态
                }];
            }];
        }];
    }];
}

#pragma mark - loadLazy
- (UIView *)backView
{
    if(!_backView){
        _backView = [[UIView alloc]init];
        _backView.layer.cornerRadius = 4.0;
        _backView.layer.masksToBounds = YES;
    }
    return _backView;
}

- (UIImageView *)iconView
{
    if(!_iconView){
        _iconView = [[UIImageView alloc]init];
        _iconView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _iconView;
}

- (UIImageView *)coverView
{
    if(!_coverView){
        _coverView = [[UIImageView alloc]init];
        _coverView.contentMode = UIViewContentModeScaleAspectFill;
        _coverView.layer.cornerRadius = 4.0;
        _coverView.layer.masksToBounds = YES;
    }
    return _coverView;
}

- (UILabel *)titleLabel
{
    if(!_titleLabel){
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = kIsIPad?FONT_SIZE(16):FONT_SIZE(14);
        _titleLabel.textColor = APPTextThemeColor;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    }
    return _titleLabel;
}

- (UILabel *)dateLabel
{
    if(!_dateLabel){
        _dateLabel = [[UILabel alloc]init];
        _dateLabel.font = kIsIPad?FONT_SIZE(14):FONT_SIZE(12);
        _dateLabel.textColor = APPTextThemeColor;
    }
    return _dateLabel;
}

- (UILabel *)descLabel
{
    if(!_descLabel){
        _descLabel = [[UILabel alloc]init];
        _descLabel.hidden = YES;
        _descLabel.textColor = UIColor.whiteColor;
        _descLabel.font = kIsIPad?FONT_SIZE(14):FONT_SIZE(12);
        _descLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _descLabel;
}

- (UIImageView *)lockView
{
    if(!_lockView){
        _lockView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_lock"]];
        _lockView.hidden = YES;
    }
    return _lockView;
}

- (UIView *)bottomView
{
    if(!_bottomView){
        _bottomView = [[UIView alloc]init];
//        _bottomView.backgroundColor = AppBgThemeColor;
//        _bottomView.alpha = 0.85;
    }
    return _bottomView;
}

@end
